package main;

import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import org.json.JSONException;
import org.json.JSONObject;

import database.DatabaseUtility;

@Path("/")
public class MainWS
{

	@POST
	@Path("/Login")
	@Produces("application/json")
	public String CheckLogin(@FormParam("credentials") String json) 
	{	
		JSONObject JsonRecived = new JSONObject(json);
		JSONObject JsonResponse = new JSONObject();
		
		DatabaseUtility db = new DatabaseUtility();
		try
		{
			if(db.CheckLogin(JsonRecived.getString("email"),JsonRecived.getString("password")))
				JsonResponse.put("status", "OK");
			else
				JsonResponse.put("status", "NO");
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		
		return JsonResponse.toString();
	}
	@POST
	@Path("/GetUserData")
	@Produces("application/json")
	public String GetUserData(@FormParam("email") String json) throws JSONException, Exception 
	{	
		DatabaseUtility db = new DatabaseUtility();
		JSONObject JsonRecived = new JSONObject(json);
		JSONObject JsonResponse = db.getUserData(JsonRecived.getString("email"));
		return JsonResponse.toString();
	}
	
	@POST
	@Path("/Registration")
	@Produces("application/json")
	public String Registration(@FormParam("user_data") String json) throws JSONException, Exception 
	{	
		DatabaseUtility db = new DatabaseUtility();
		JSONObject JsonRecived = new JSONObject(json);
		boolean response = db.Registration(JsonRecived.getString("email"),JsonRecived.getString("name"),JsonRecived.getString("surname"),JsonRecived.getString("date_birth"),JsonRecived.getString("gender"),JsonRecived.getString("city"),JsonRecived.getString("photo"));
		if(response)
			return "success";
		else
			return "fail";
	}
}
